#include "projecttaskmodel.h"

ProjectTaskModel::ProjectTaskModel(QObject *parent)
    : QAbstractTableModel{parent}
{

}

/*reimp*/ int ProjectTaskModel::rowCount(const QModelIndex &index)
{
    return QModelIndex().isValid() ? 0 : m_tasks.size();
}
/*reimp*/ int ProjectTaskModel::columnCount(const QModelIndex &index)
{
    return QModelIndex().isValid() ? 0 : 6;
}
/*reimp*/ QVariant ProjectTaskModel::data(const QModelIndex &index, int role)
{
    QDate s1,s2;
    QString s3;

    role = Qt::DisplayRole;
    if (index.isValid() && index.row() < rowCount() && index.column() < columnCount()) {
        switch (index.column()) {
        case 0:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(m_tasks[index.row()].title)
                       : QVariant();
        case 1:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(( int )KDGantt::TypeTask)
                       : QVariant();
        case 2:
            if(m_tasks[index.row()].start<=m_startDate)
                s1=m_startDate;
            else
                s1=m_tasks[index.row()].start;
            return (role == KDGantt::StartTimeRole || role == Qt::DisplayRole)
                       ? QVariant::fromValue(s1)
                       : QVariant();
        case 3:
            if(m_tasks[index.row()].end>=m_endDate)
                s2=m_endDate;
            else
                s2=m_tasks[index.row()].end;
            return (role == KDGantt::EndTimeRole || role == Qt::DisplayRole)
                       ? QVariant::fromValue(s2)
                       : QVariant();
        case 4:
            if(m_modelFlag==1)
                s3= m_tasks[index.row()].developName;
            else if(m_modelFlag==2)
                s3= m_tasks[index.row()].projectName;
            else
                s3="-";
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(s3)
                       : QVariant();
        case 5:
            return (role == Qt::DisplayRole)
                       ? QVariant::fromValue(m_tasks[index.row()].consumed)
                       : QVariant();
        }
    }

    return QVariant();
}

void ProjectTaskModel::setModelData(const QList<ProjectTask> &datas)
{
    // qDebug()<<"setModelData:"<<QDateTime::currentDateTime();
    //重置model数据之前调用beginResetModel，此时会触发modelAboutToBeReset信号
    beginResetModel();
    //重置model中的数据
    m_tasks=datas;
    //数据设置结束后调用endResetModel，此时会触发modelReset信号
    endResetModel();
    //注意：reset model后，选中的item会失效，我们可以自己写保存和恢复选中项的逻辑
    //如果表的行列数是固定的，只是数据变更了，我们可以用 dataChanged 信号来请求刷新。
    //emit dataChanged(index(0,0),index(RowMax-1,ColMax-1),QVector<int>());
}
bool ProjectTaskModel::setHorHeaderData(const QList<QString> &headers)
{
    // qDebug()<<"setHorHeaderData:"<<QDateTime::currentDateTime();
    m_horHeaderData=headers;
    emit headerDataChanged(Qt::Horizontal, 0, headers.count()-1);
    return true;
}

QVariant ProjectTaskModel::headerData(int section, Qt::Orientation orientation, int role)
{
    //   qDebug()<<"headerData:"<<section<<QDateTime::currentDateTime();
    if(orientation == Qt::Horizontal){
        //这里我们只设置居中对齐和文本
        if (role == Qt::DisplayRole){
            //这里把横项列表头的文本设计为可以设置的
            if(section>=0 && section<m_horHeaderData.count())
                return m_horHeaderData.at(section);
            return QString("Col %1").arg(section + 1);
        }else if(role == Qt::TextAlignmentRole){
            return Qt::AlignCenter;
        }
    }else{
        if (role == Qt::DisplayRole){
            qDebug()<<"headerdata:"<<section;
            return QString("Row %1").arg(section + 1);
        }
        else if(role == Qt::TextAlignmentRole)
            return Qt::AlignCenter;
    }
    return QVariant();
}

bool ProjectTaskModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    qDebug()<<"setHeaderData:"<<QDateTime::currentDateTime();
    if (value != headerData(section, orientation, role)) {
        m_horHeaderData[section] = value.toString();
        emit headerDataChanged(orientation, section, section);
        return true;
    }
    return false;
}

void ProjectTaskModel::moveRow(int from, int to)
{
    if (from == to)
        return;
    if (from >= m_tasks.size() || to >= m_tasks.size() + 1)
        return;

    if (beginMoveRows(QModelIndex(), from, from, QModelIndex(), to)) {
        m_tasks.move(from, to);
        endMoveRows();
    } else {
        assert(0);
    }
}
